﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using T163.Open.Api.Model;
using Newtonsoft.Json.Linq;

namespace TestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestUserDeserialize()
        {
            string userStr = @"{
                                    name: ""开放平台"",
                                    location: """",
                                    id: ""2229543033866779776"",
                                    description: """",
                                    status: {
                                        id: ""8619344141274950063"",
                                        source: ""网易微博"",
                                        text: ""这是网易微博开放平台"",
                                        created_at: ""Fri Jul 16 11:52:45 +0800 2010"",
                                        truncated: false,
                                        in_reply_to_status_id: null,
                                        in_reply_to_user_id: null,
                                        in_reply_to_screen_name: null,
                                        in_reply_to_user_name: null,
                                    },
                                    screen_name: ""openAPI"",
                                    gender: ""0"",
                                    friends_count: ""0"",
                                    followers_count: ""0"",
                                    statuses_count: ""1"",
                                    created_at: ""Fri Jul 16 11:52:23 +0800 2010"",
                                    favourites_count: ""0"",
                                    profile_image_url: ""http://126.fm/15fB1f"",
                                    url: """",
                                    blocking: false,
                                    following: false,
                                    followed_by: false,
                                    verified: false
                            }";

            Users user = JsonConvert.DeserializeObject<Users>(userStr);

            Assert.IsNotNull(user);
            Assert.AreEqual("openAPI", user.ScreenName);
            Assert.AreEqual("0", user.Gender);
            Assert.IsFalse(user.Verified);
        }

        [TestMethod]
        public void TestStatuesDeserialize()
        {
            string statuesStr = @"{
                                      id: ""-7356608697955139661"",
                                      source: ""网易微博"",
                                      user: {
                                          name: ""oxygen"",
                                          location: ""重庆市,巫山县"",
                                          id: ""-3586996203846210229"",
                                          description: ""☑互联网民工   ☑无趣   ☑Gtalk/Email：i@ecvip.org"",
                                          screen_name: ""oxygen"",
                                          gender: ""1"",
                                          friends_count: ""681"",
                                          followers_count: ""11397"",
                                          statuses_count: ""34"",
                                          created_at: ""Wed Jul 28 16:34:47 +0800 2010"",
                                          favourites_count: ""0"",
                                          profile_image_url: ""http://126.fm/2C8bLE"",
                                          url: ""http://ecvip.org/"",
                                          verified: false,
                                          geo_enabled: true
                                          },
                                      text: ""@想飞的鱼 FFFF KKK"",
                                      created_at: ""Wed Jul 28 17:19:06 +0800 2010"",
                                      in_reply_to_status_id: ""7492662492981770300"",
                                      in_reply_to_user_id: ""8399533907599838042"",
                                      in_reply_to_screen_name: ""361"",
                                      in_reply_to_status_text: ""foo"",
                                      retweet_count: ""0"",
                                      comments_count: ""0"",
                                      geo:
                                          {
                                           type: ""point"",
                                           coordinates: [39.9861, 116.315]
                                           },
                                      venue:
                                           {
                                            id:""12345"",
                                            name: ""五道口购物中心"",
                                            address:""北京市海淀区成府路"",
                                            city:""北京市"",
                                            province: ""null"",
                                            state:""中国"",
                                            coordinates: [39.9861, 116.315] 
                                            },
                                      favorited_at: null,
                                      in_reply_to_user_name: ""想飞的鱼"",
                                      truncated: false,
                                      favorited: false,
                                      root_in_reply_to_status_id: ""3638308196973752634"",
                                      is_retweet_by_user: false,
                                      retweet_user_id:null
                                      }";

            Statues statues = JsonConvert.DeserializeObject<Statues>(statuesStr);

            Assert.IsNotNull(statues);
            Assert.AreEqual("7492662492981770300", statues.InReplyToStatusID);
            Assert.IsNull(statues.FavoritedAt);
            Assert.AreEqual("3638308196973752634", statues.RootInReplyToStatusId);

            Assert.IsNotNull(statues.User);
            Assert.AreEqual("oxygen", statues.User.Name);
            Assert.AreEqual(11397, statues.User.FollowersCount);
            Assert.AreEqual(34, statues.User.StatusesCount);
        }

        [TestMethod]
        public void TestStatuesListDeserialize()
        {
            string statuesList = @"[{
                                      id: ""-7356608697955139661"",
                                      source: ""网易微博"",
                                      user: {
                                          name: ""oxygen"",
                                          location: ""重庆市,巫山县"",
                                          id: ""-3586996203846210229"",
                                          description: ""☑互联网民工   ☑无趣   ☑Gtalk/Email：i@ecvip.org"",
                                          screen_name: ""oxygen"",
                                          gender: ""1"",
                                          friends_count: ""681"",
                                          followers_count: ""11397"",
                                          statuses_count: ""34"",
                                          created_at: ""Wed Jul 28 16:34:47 +0800 2010"",
                                          favourites_count: ""0"",
                                          profile_image_url: ""http://126.fm/2C8bLE"",
                                          url: ""http://ecvip.org/"",
                                          verified: false,
                                          geo_enabled: true
                                          },
                                      text: ""@想飞的鱼 FFFF KKK"",
                                      created_at: ""Wed Jul 28 17:19:06 +0800 2010"",
                                      in_reply_to_status_id: ""7492662492981770300"",
                                      in_reply_to_user_id: ""8399533907599838042"",
                                      in_reply_to_screen_name: ""361"",
                                      in_reply_to_status_text: ""foo"",
                                      retweet_count: ""0"",
                                      comments_count: ""0"",
                                      geo:
                                          {
                                           type: ""point"",
                                           coordinates: [39.9861, 116.315]
                                           },
                                      venue:
                                           {
                                            id:""12345"",
                                            name: ""五道口购物中心"",
                                            address:""北京市海淀区成府路"",
                                            city:""北京市"",
                                            province: ""null"",
                                            state:""中国"",
                                            coordinates: [39.9861, 116.315] 
                                            },
                                      favorited_at: null,
                                      in_reply_to_user_name: ""想飞的鱼"",
                                      truncated: false,
                                      favorited: false,
                                      root_in_reply_to_status_id: ""3638308196973752634"",
                                      is_retweet_by_user: false,
                                      retweet_user_id:null
                                      },
                                     {
                                      id: ""-7356608697955139662"",
                                      source: ""网易微博"",
                                      user: {
                                          name: ""oxygen"",
                                          location: ""重庆市,巫山县"",
                                          id: ""-3586996203846210229"",
                                          description: ""☑互联网民工   ☑无趣   ☑Gtalk/Email：i@ecvip.org"",
                                          screen_name: ""oxygen"",
                                          gender: ""1"",
                                          friends_count: ""681"",
                                          followers_count: ""11397"",
                                          statuses_count: ""34"",
                                          created_at: ""Wed Jul 28 16:34:47 +0800 2010"",
                                          favourites_count: ""0"",
                                          profile_image_url: ""http://126.fm/2C8bLE"",
                                          url: ""http://ecvip.org/"",
                                          verified: false,
                                          geo_enabled: true
                                          },
                                      text: ""@想飞的鱼 FFFF KKK 垃圾"",
                                      created_at: ""Wed Jul 28 17:19:06 +0800 2010"",
                                      in_reply_to_status_id: ""7492662492981770300"",
                                      in_reply_to_user_id: ""8399533907599838042"",
                                      in_reply_to_screen_name: ""361"",
                                      in_reply_to_status_text: ""foo"",
                                      retweet_count: ""0"",
                                      comments_count: ""0"",
                                      geo:
                                          {
                                           type: ""point"",
                                           coordinates: [39.9861, 116.315]
                                           },
                                      venue:
                                           {
                                            id:""12345"",
                                            name: ""五道口购物中心"",
                                            address:""北京市海淀区成府路"",
                                            city:""北京市"",
                                            province: ""null"",
                                            state:""中国"",
                                            coordinates: [39.9861, 116.315] 
                                            },
                                      favorited_at: null,
                                      in_reply_to_user_name: ""想飞的鱼"",
                                      truncated: false,
                                      favorited: false,
                                      root_in_reply_to_status_id: ""3638308196973752634"",
                                      is_retweet_by_user: false,
                                      retweet_user_id:null
                                      }]";

            IList<Statues> statues = JsonConvert.DeserializeObject<IList<Statues>>(statuesList);

            Assert.AreEqual(2, statues.Count);
        }

        [TestMethod]
        public void TestDateTimeConvert()
        {
            string time = "Fri Jul 16 11:52:23 +0800 2010";
            string format = "ddd MMM dd HH:mm:ss zzzz yyyy";

            DateTime myTime;

            myTime = DateTime.ParseExact(time, format, CultureInfo.InvariantCulture);

            Console.WriteLine(DateTime.ParseExact(time, format, CultureInfo.InvariantCulture).ToString("yyyy/M/d h:m"));

            Assert.IsNotNull(myTime);
            Assert.AreEqual(2010, myTime.Year);
            Assert.AreEqual(new TimeSpan(11, 52, 23), myTime.TimeOfDay);
        }

        [TestMethod]
        public void TestAnonymousType()
        {
            string str = @"{
                               ""upload_image_url"": ""http://126.fm/2C8bLE""
                            }";

            var uploadImage = new { upload_image_url = "image_address" };
            var image = JsonConvert.DeserializeAnonymousType(str, uploadImage);

            var jobj = JObject.Parse(str);
            Console.WriteLine(jobj["upload_image_url"].Value<string>());

            Assert.AreEqual("http://126.fm/2C8bLE", image.upload_image_url);

            int a = 1;
            Console.WriteLine(a << 2);
        }

        [TestMethod]
        public void TestFriendshipDeserialize()
        {
            string str = @"{
                             source: {
                                id: ""3407397647969193784"",
                                screen_name: ""Tux"",
                                following: false,
                                followed_by: true
                                },
                             target: {
                                id: ""-7325997749471485394"",
                                screen_name: ""001"",
                                following: true,
                                followed_by: false
                                }
                            }";

            Friendship fship = JsonConvert.DeserializeObject<Friendship>(str);

            Assert.AreEqual("Tux", fship.Source.ScreenName);
            Assert.AreEqual("-7325997749471485394", fship.Target.Id);
        }

        [TestMethod]
        public void Test()
        {
            List<Tst> lst = new List<Tst>(){
                new Tst { key = "key1", value = "value1" },
                new Tst { key = "key2", value = "" },
                new Tst { key = "key3", value = "value3"}
            };

            IEnumerable<Tst> noneEmptyValue = from t in lst
                                              where !string.IsNullOrEmpty(t.value)
                                              select t;

            Assert.AreEqual(2, noneEmptyValue.ToList().Count);
            Assert.AreEqual("value1", noneEmptyValue.ToList()[0].value);
            Assert.AreEqual("value3", noneEmptyValue.ToList()[1].value);
        }

        [TestMethod]
        public void TestTrends()
        {
            string str = @"{ trends:
                                    [
                                        {
                                        name:""抗旱"",
                                        query:""抗旱"",
                                        },
                                        {
                                        name:""情人节表白"",
                                        url:""http://t.163.com/zt/valentine"",
                                        }
                                    ]
                                   
                             }";

            var Trends = new { trends = new List<Trend>() };

            List<Trend> lst = JsonConvert.DeserializeAnonymousType(str, Trends).trends;

            Assert.IsNotNull(lst);
            Assert.AreEqual(2, lst.Count);
        }

        [TestMethod]
        public void TestFriends()
        {
            string str = @"{
                            next_cursor:-1,
                            users:[{name:""李寅"",location:""北京市,海淀区"",id:""428492875498758184"",description:"""",status:{id:""-441606637625369132"",source:""<a target=\""_blank\"" href=\""http://www.dev123.net\"">YiBo.Android版</a>"",created_at:""Sat May 07 00:35:04 +0800 2011"",text:""在看流星花园，想起妈妈当时总是把杉菜说成酸菜，一晃十年都过去了。。酸菜也嫁人了，变成咸菜了。。。"",truncated:false,in_reply_to_status_id:null,in_reply_to_user_id:null,in_reply_to_screen_name:null,in_reply_to_user_name:null},created_at:""Wed Jan 20 12:16:40 +0800 2010"",profile_image_url:""http://oimageb5.ydstatic.com/image?w=48&h=48&url=http%3A%2F%2F126.fm%2F23xGpQ"",screen_name:""R7loser"",gender:""1"",url:"""",followers_count:""4621"",friends_count:""582"",favourites_count:""10"",statuses_count:""2067"",realName:null,sysTag:null,icorp:null,darenRec:null,userTag:[""80后"",""数码控"",""IT民工"",""动漫"",""经济学"",""历史""],blocking:false,verified:false,following:true,followed_by:false},{name:""王有华"",location:""北京市,海淀区"",id:""-4595732624339855088"",description:""开放平台"",status:{id:""-8311549878011198098"",source:""网易微博"",created_at:""Sat May 07 00:00:54 +0800 2011"",text:""你是不知道胖纸的苦恼啊||@国德伟 ：一般刚吃饱的人就这么说，明天的这个时候就忘记了"",truncated:false,in_reply_to_status_id:""5526753987785164539"",in_reply_to_user_id:""-5420075839861508631"",in_reply_to_screen_name:""gdwchina"",in_reply_to_user_name:null},created_at:""Fri Aug 06 17:29:45 +0800 2010"",profile_image_url:""http://oimagea6.ydstatic.com/image?w=48&h=48&url=http%3A%2F%2F126.fm%2F4X5NYQ"",screen_name:""kerby"",gender:""0"",url:"""",followers_count:""538"",friends_count:""190"",favourites_count:""7"",statuses_count:""2139"",realName:""减肥ing"",sysTag:null,icorp:null,darenRec:null,userTag:[""80后"",""IT民工"",""美剧"",""减肥ING"",""网球"",""善良"",""爱上网"",""日照""],blocking:false,verified:false,following:true,followed_by:false}],previous_cursor:""-1""}";

            var fans = new { next_cursor = "1", users = new List<Users>(), previous_cursor = "-1" };
            List<Users> lst = JsonConvert.DeserializeAnonymousType(str, fans).users;

            Assert.IsNotNull(lst[0].UserTag);
            Assert.AreEqual("80后", lst[0].UserTag[0]);
            Assert.IsNotNull(lst);
            Assert.AreEqual(2, lst.Count);
            Assert.AreEqual("kerby", lst[1].ScreenName);
        }

        [TestMethod]
        public void TestRetweets()
        {
            string str = @"[{
                            created_at:null,
                            retweeted_status:{
                                id:""5406542426840414873"",
                                source:""<a target=_blank href=http://www.tgoose.com>tGoose</a>"",
                                user:{ name: ""Jeek"",
                                    location:""北京,海淀"",
                                    id:""1900421553912645989"",
                                    description:""Hi,I'm a geek. I currently work for plx I spend most of my time in software development spaces dealing with Microsoft technologies."",
                                    created_at:""Sat Jul 31 15:41:00 +0800 2010"",
                                    profile_image_url:""http://oimagea1.ydstatic.com/image?w=48&h=48&url=http%3A%2F%2F126.fm%2F43xaiY"",
                                    screen_name:""Jeek"",
                                    gender:""0"",
                                    url:""http://blog.designbased.net"",
                                    followers_count:""13"",
                                    friends_coun:""1"",
                                    favourites_count:""2"",
                                    statuses_count:""12"",
                                    profile_image_url_large:null,
                                    profile_image_url_small:null,
                                    realName:null,
                                    sysTag:null,
                                    icorp:null,
                                    darenRec:null,
                                    verified:false,
                                    geo_enable:true
                                },
                            flag:""NORMAL"",
                            created_at:""Wed Apr 13 21:26:01 +0800 2011"",
                            text:""由测试程序发布的微博"",
                            truncated:false,
                            retweet_count:""2"",
                            in_reply_to_status_id:null,
                            in_reply_to_user_id:null,
                            in_reply_to_screen_name:null,
                            in_reply_to_user_name:null,
                            in_reply_to_status_text:null,
                            favorited:false,
                            favorited_at:null,
                            root_in_reply_to_status_id:null,
                            cursor_id:""5406542426840414873:1302701215695"",
                            retweet_user_id:null,
                            retweet_user_name:null,
                            retweet_user_screen_name:null,
                            retweet_created_at:null,
                            root_in_reply_to_user_id:null,
                            root_in_reply_to_screen_name:null,
                            root_in_reply_to_user_name:null,
                            root_in_reply_to_status_text:null,
                            comments_count:""2"",
                            is_retweet_by_user:false,
                            venue:null
                          }}]";

            var retweets = new { created_at = "created_at", retweeted_status = new Statues() };
            var retweets1 = new { created_at = "created_at1", retweeted_status = new Statues() };

            var ress = new[] { retweets, retweets1 };

            var lst = JsonConvert.DeserializeAnonymousType(str, ress);

            foreach (var rt in lst)
            {
                Console.WriteLine(rt.retweeted_status.Text);
            }

            //Assert.AreEqual("5406542426840414873", rett.ID);
            //Assert.AreEqual("Jeek", rett.User.Name);
        }

//        [TestMethod]
//        public void TestUsersArray()
//        {
//            string str = @"{
//                                "users":[{"reason":"TA在你附近","name":"弥赛亚丶哥特","location":"北京市,朝阳区","id":"-6994381408252843582","description":"","status":{"id":"-3489193647710533459","source":"网易微博","created_at":"Mon Aug 16 20:35:56 +0800 2010","text":"听听说说","truncated":false,"in_reply_to_status_id":null,"in_reply_to_user_id":null,"in_reply_to_screen_name":null,"in_reply_to_user_name":null},"created_at":"Mon Aug 16 20:16:14 +0800 2010","profile_image_url":"http://oimagec1.ydstatic.com/image?w=48&h=48&url=http%3A%2F%2F126.fm%2F47ymI7","screen_name":"freeman_521","gender":"1","url":"","followers_count":"5","friends_count":"10","favourites_count":"0","statuses_count":"1","followed_by":false,"following":false,"realName":null,"sysTag":null,"icorp":null,"darenRec":null,"userTag":null,"blocking":false,"verified":false}]}";
//        }
    }
    
    public class Tst
    {
        public string key;

        public string value;
    }
}
